﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evolition
{
    class Program
    {
        const int ArNum = 30;
        const int CellNum = 2;
        const int FreeNum = 0;
        static int[,] newarr = new int[ArNum, ArNum];
        static int[,] new2arr = new int[ArNum, ArNum];


        static void ShowTheArr()
        {
            string str;

            if (Console.WindowHeight <= ArNum) Console.WindowHeight = ArNum+10;
            if (Console.WindowWidth <= ArNum+1) Console.WindowWidth = ArNum+10;

            int i, j;
            for (i = 0; i < ArNum; i++)
            {
                str = "";
                for (j = 0; j < ArNum; j++)
                    if (newarr[i, j] == 0)
                        str += " ";
                    else
                        str += "*";
                Console.WriteLine(str);
            }

        }

        static void Transform(int numi, int numj, int flag = 0) 
        //if flag=0 it means that current cell is empty-cell
        //if flag=1 it means that current cell is *-cell
        {
            int i, j;
            int counter = 0;
            for (i = numi - 1; i <= numi + 1; i++)
            {
                if (i < 0 || i > ArNum - 1) continue;
                for (j = numj - 1; j <= numj + 1; j++)
                {
                    if (j < 0 || j > ArNum - 1) continue;
                    counter = counter + (newarr[i, j] == flag ? 0 : 1);
                }
            }
            switch (flag)
            {
                case 0:
                    if (counter >= CellNum) new2arr[numi, numj] = 1;
                    else new2arr[numi, numj] = newarr[numi, numj];
                    break;
                case 1:
                    if (counter <= FreeNum) new2arr[numi,numj]=0;
                    else new2arr[numi, numj] = newarr[numi, numj];
                    break;
            }

        }


        static void Main(string[] args)
        {
            Random RandomNubmer = new Random();
            int i, j;
            int[,] point;

            for (i = 0; i < ArNum; i++)
                for (j = 0; j < ArNum; j++)
                    newarr[i, j] = RandomNubmer.Next(2);
            ShowTheArr();
            Console.WriteLine("Press 'q' to exit");
            ConsoleKeyInfo key = new ConsoleKeyInfo();
            //key=Console.ReadKey(true);
            while (key.KeyChar != 'q')
            {
                
                key = Console.ReadKey(true);
                if (key.KeyChar == ' ')
                {
                    for (i = 0; i < ArNum; i++)
                        for (j = 0; j < ArNum; j++)
                            Transform(i, j, newarr[i, j]);
                    Console.Clear();
                    ShowTheArr();
                    point = newarr;
                    newarr = new2arr;
                    new2arr = point;
                    //change the pointer between 2 arrays
                    Console.WriteLine("Press 'q' to exit");
                }
            }
        }

    }
}
